package com.dingding.study.Event;

import com.dingding.study.Event.service.EventProcessException;
import com.google.common.annotations.Beta;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @program: spring-learning
 * @description:
 * @author: liugongding
 * @create: 2021-09-13 17:24
 **/
@Beta
public class DzjEventBus extends EventBus {

    private SubscriberExceptionHandler exceptionHandler;

    public DzjEventBus() {
        super();
    }

    public DzjEventBus(String identifier) {
        super(identifier);
    }

    public DzjEventBus(SubscriberExceptionHandler subscriberExceptionHandler) {
        super(subscriberExceptionHandler);
        this.exceptionHandler = subscriberExceptionHandler;
    }

//    void dispatch(Object event, EventSubscriber wrapper) {
//        try {
//            wrapper.handleEvent(event);
//        } catch (InvocationTargetException ex) {
//            throw new InternalException(ex);
//        }
//    }

    void handleSubscriberException(Throwable ex, SubscriberExceptionContext context) {
        checkNotNull(ex);
        checkNotNull(context);
        if (exceptionHandler != null) {
            exceptionHandler.handleException(ex, context);
        } else {
            throw new EventProcessException(ex);
        }
    }
}